*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {


*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "tol_cer tol_fis tol_the_iep tol_bro_iep" 

* irf horizon
local H 5
local F 0

* lags
local LS 2 

*-------------------------------------------------------------------------------------------------------------

*_________ GET DATA ____________
use "data/working files/prepared.dta", clear /*read in data*/


*____ DECLARE TIME SERIES _____
gen id =1
tsset id year, yearly



*_________________ LOCAL PROJECTIONS _________________

local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"


	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
	gen irf_`v' = . if _n<=`H'+1
	
	gen se_`v' = . if _n<=`H'+1

	gen sig_`v' = . if _n<=`H'+1
	gen ci_up_`v' = . if _n<=`H'+1
	gen ci_lo_`v' = . if _n<=`H'+1
	gen ci_up90_`v' = . if _n<=`H'+1
	gen ci_lo90_`v' = . if _n<=`H'+1
			

	
	***_________ ESTIMATE COEFFICIENTS
	
	forvalues y = 0/`H' {
		n display "projection `y'"
		
		local F = 0
		local LS = 1
		newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').percloss_netN, lag(5) force

		* store IMPULSE RESPONSES and STANDARD ERRORS	
		lincom percloss_netN
		
		replace irf_`v' = r(estimate) if _n==`y'+1 /* coefficients */
		replace se_`v' = r(se) if _n==`y'+1 /* standard errors */

		} //horizon			
	} // vars


	
local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"

	***_________ CONFIDENCE INTERVALS
	replace ci_up_`v' = irf_`v' + 1*se_`v' if _n <= `H'+1
	replace ci_lo_`v' = irf_`v' - 1*se_`v' if _n <= `H'+1
	replace ci_up90_`v' = irf_`v' + 1.64*se_`v' if _n <= `H'+1
	replace ci_lo90_`v' = irf_`v' - 1.64*se_`v' if _n <= `H'+1
	
	replace sig_`v' =  irf_`v' if ci_up90_`v' < 0 | ci_lo90_`v' > 0

}

* plot prerequisites
gen time = _n-1 if(_n<=`H'+1) 
gen time2 = time + 0.1 

drop if time > `H'
keep time time* irf_* ci_up_* ci_lo_*  ci_up90_* ci_lo90_* se_* sig_*
order irf_* ci_up_* ci_lo_*  time ci_up90_* ci_lo90_* sig_*
drop if time == . /*so quarters uniquely identifies each observation*/
gen zero = 0 if time!=.



local lsize "5.5"
local tsize "6"
local legsize "5.5"

local zline "medthin"
local irfline "medlarge"
local msize "vlarge"


//Primary consumption and transit trade
twoway  (line zero time, lcolor(black) lwidth(`zline')) ///
		(scatter irf_tol_cer time, color(black) msymbol(circle_hollow) msize(`msize')) ///
		(line irf_tol_cer time, lcolor(black) lpattern(dot) lwidth(`irfline')) ///
		(scatter irf_tol_fis time, color(gs8) msymbol(diamond_hollow) msize(`msize')) ///
		(line irf_tol_fis time, lcolor(gs8) lpattern(dot) lwidth(`irfline')) ///
		(scatter sig_tol_cer time, color(black) msymbol(circle) msize(`msize')) ///
		(scatter sig_tol_fis time, color(gs8) msymbol(diamond) msize(`msize')), ///
		title("Primary goods consumption & transit", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-8(2)4, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4) col(1) pos(7) ring(0) lab(2 "Cereals") lab(4 "Fish") region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g5, replace) nodraw
		
		
//Services 
twoway  (line zero time, lcolor(black) lwidth(`zline')) ///
		(scatter irf_tol_bro time, color(black) msymbol(circle_hollow) msize(`msize')) ///
		(line irf_tol_bro time, lcolor(black) lpattern(dot) lwidth(`irfline')) ///
		(scatter irf_tol_the time, color(gs8) msymbol(diamond_hollow) msize(`msize')) ///
		(line irf_tol_the time, lcolor(gs8) lpattern(dot) lwidth(`irfline')) ///
		(scatter sig_tol_bro time, color(black) msymbol(circle) msize(`msize')) ///
		(scatter sig_tol_the time, color(gs8) msymbol(diamond) msize(`msize')), ///
		title("Service consumption", size(`tsize')) xtitle("Year", size(`lsize')) ytitle("Percent", height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(-8(2)4, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(size(`legsize') symxsize(*.5) order(2 4) col(1) pos(7) ring(0) lab(2 "Brothel") lab(4 "Theater") region(color(white%0))) ///
		graphregion(color(white)) plotregion(color(white)) name(g6, replace) nodraw

		

graph combine g5 g6, name(LP_disag_output2, replace) cols(2) imargin(medlarge) plotregion(color(white)) graphregion(color(white)) 
graph display LP_disag_output2, ysize(4.5) xsize(10) 
graph export "results/FigureB1.pdf", replace 	




} // qui


*_________________________________________

			

			
